<?php

/**
 * 执行各种脚本方法
 */

namespace app\common;

class Script
{
    //非阻塞执行替换热词模型脚本
    protected $replaceHotWordModel;
    //修改asr model 脚本
    protected $exchangeAsrModel;
    //修改非实时进程数量
    protected $changeNonRealProcessNum;
    //获取文本摘要
    protected $getAbstract;

    public function __construct()
    {
        //初始化
        $this->replaceHotWordModel = 'sudo bash ' . config('huisheng.wordReplaceModel');
        $this->exchangeAsrModel = "sudo " . '/home/change_model.sh';
        $this->changeNonRealProcessNum = "sudo " . '/home/speed_up_transfer.sh';
        $this->getAbstract =  "python ../script/summary.py ";
    }
    public function computeWer($reference, $result)
    {
        if ($reference) {
            $filename = uniqid();
            //范本文件
            $reference_path = config('share_path') . config('testing.computeWer') . $filename . '.reference';
            //对比文件
            $result_path = config('share_path') . config('testing.computeWer') . $filename . '.result';
            $this->putFile($reference_path, $this->computeWerformat($reference));
            $this->putFile($result_path, $this->computeWerformat($result));
            //执行脚本
            exec("sudo " . config('share_path') . config('testing.computeScript') . " $reference_path" . " $result_path");
            $wer_result = $result_path . "_cer";
            $result = $this->computeWerResultFormat(file($wer_result)) >= 100 ? 0 : 100 - $this->computeWerResultFormat(file($wer_result));
            return (float) $result;
        } else {
            return (float) 0;
        }
    }

    //格式化对比字符串
    public function computeWerformat($str)
    {
        return '0 ' . $str;
    }

    //生成文件
    public function putFile($path, $data)
    {
        $fp = fopen($path, "w+");
        if (flock($fp, LOCK_EX)) {
            fwrite($fp, $data);
            flock($fp, LOCK_UN);
            fclose($fp);
            return true;
        }
        fclose($fp);
        return false;
    }
    //对比结果处理
    public function computeWerResultFormat($arr)
    {
        //
        $_str = $arr[1];
        $result = substr($_str, 4, strpos($_str, " [ "));
        return (float) $result;
    }

    //信噪比计算
    public function computeNSR()
    {
    }

    //热词启动替换模型
    public function wordReplaceModel($replaceStr)
    {
        //打开文件 清空内容 输入热词数据
        $file = fopen(config('huisheng.online_path'), 'w');
        if (flock($file, LOCK_EX)) {
            fwrite($file, $replaceStr);
            fclose($file);
        } else {
            fclose($file);
            return false;
        }
        $cmd = $this->replaceHotWordModel . " " . config('huisheng.online_path') . " > /dev/null 2>&1&";

        @exec($cmd);
        return true;
    }

    //ASR服务器切换model
    public function asrExchangeModel($modelType)
    {
        $cmd = $this->exchangeAsrModel . " $modelType";
        @exec($cmd);
        return true;
    }

    //非实时转写进程修改
    public function nonrealTransformModification($processCount)
    {
        $cmd = $this->changeNonRealProcessNum . " $processCount";
        @exec($cmd);
        return true;
    }

    //获取摘要  
    public function getTransformContentAbstract($transformStr)
    {
        //
        $num = ceil(strlen($transformStr) / (3 * 2000));
        //shell命令获取摘要
        $commond = $this->getAbstract . $num . ' ' . $transformStr;
        exec($commond, $result);
        unset($result[0]);
        return $result;
    }

    //去除语气词
    public function removeModalWords($original)
    {
        $param = [
            'session_id' => intval(input('param.id')),
            'text' => $original
        ];
        $result = json_decode(curl_post(config('huisheng.MODAL_URL'), json_encode($param)), 1);
        if (!isset($result['status'])) {
            throw new \Exception("请求语气词接口失败");
        } else if ($result['status'] != 200) {
            throw new \Exception($result['description']);
        }
        return $result['result'];
    }

    //替换敏感词
    public function removeSensitiveWords($original)
    {
        $param = [
            'id' => intval(input('param.id')),
            'texts' => $original
        ];
        $result = json_decode(curl_post(config('huisheng.SENSITIVE_URL'), json_encode($param)), 1);
        if (!isset($result['status'])) {
            throw new \Exception("请求敏感词接口失败");
        } else if ($result['status'] != 200) {
            throw new \Exception($result['description']);
        }
        return $result['result'];
    }
}
